/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package ee.ioc.phon.ohos.speechutils.utils;

import ee.ioc.phon.ohos.speechutils.ResourceTable;
import ohos.aafwk.ability.Ability;
import ohos.agp.components.DirectionalLayout;
import ohos.agp.components.LayoutScatter;
import ohos.agp.components.Text;
import ohos.agp.utils.LayoutAlignment;
import ohos.agp.window.dialog.ToastDialog;

/**
 * 描述 显示Toast
 *
 * @author name
 * @since 2021-04-16
 */
public class DialogUtils {
    private static final int NUM50 = -50;
    private static final int NUM3000 = 3000;

    /**
     * 构造
     */
    private DialogUtils() {
    }

    /**
     * 显示Toast
     *
     * @param msg 显示信息
     * @param ability Ability
     */
    public static void showToast(String msg, Ability ability) {
        DirectionalLayout layout = (DirectionalLayout) LayoutScatter.getInstance(ability)
                .parse(ResourceTable.Layout_layout_toast_and_image, null, false);
        Text text = (Text) layout.findComponentById(ResourceTable.Id_msg_toast);
        text.setText(msg);
        new ToastDialog(ability)
                .setComponent(layout)
                .setSize(DirectionalLayout.LayoutConfig.MATCH_PARENT, DirectionalLayout.LayoutConfig.MATCH_CONTENT)
                .setAlignment(LayoutAlignment.BOTTOM)
                .setOffset(0, NUM50)
                .setDuration(NUM3000)
                .show();
    }
}
